#!/bin/bash

#/usr/bin/expect << eof
#set timeout 10
#spawn ssh-keygen
#expect "Enter file in which to save the key (/root/.ssh/id_rsa):"
#send "\n"
#expect "Enter passphrase (empty for no passphrase):"
#send "\n"
#expect "Enter same passphrase again:"
#send "\n"
#spawn ssh-copy-id root@192.168.1.129
#expect "Are you sure you want to continue connecting (yes/no)?"
#send "yes\n"
#expect "root@192.168.1.128's password:"
#send "123456\n"
#expect eof
#eof

# 判断是否存在密钥
if [ -f ~/.ssh/id_rsa ]; then
   rm -rf ~/.ssh/id_rsa*
fi

# 生成密钥对
ssh-keygen -t rsa -f ~/.ssh/id_rsa -P "" &> /dev/null



# 判断known_hosts文件中是否包含对方主机的密钥
> ~/.ssh/known_hosts

for i in 129 130; do
/usr/bin/expect << eof
set timeout 10
spawn ssh-copy-id root@192.168.1.$i
expect "(yes/no)?"
send "yes\n"
expect "password:"
send "123456\n"
expect eof
eof
done


echo "-------------------------------------------"
# 验证
for i in 129 130; do
   ssh root@192.168.1.$i hostname
done
